﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Hiver.Data;
using Hiver.Entity;

namespace Hiver.Business.Impl
{
    /// <summary>
    /// 用户服务类
    /// </summary>
    public class UserService : IUserService
    {
        /// <summary>
        /// 通用数据仓库实例
        /// </summary>
        private Repository<UserInfo> _repository;

        /// <summary>
        /// 用户数据仓库
        /// </summary>
        public IUserRepository _userRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userRepository">依赖注入</param>
        public UserService(IUserRepository userRepository)
        {
            _repository = new Repository<UserInfo>();
            _userRepository = userRepository;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public UserInfo Login(string userName, string password)
        {
            return _userRepository.Login(userName, password);
        }

        /// <summary>
        /// 获得数据分页
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<UserInfo> GetSearchPageList(Expression<Func<UserInfo, bool>> query, int pageIndex, int pageSize)
        {
            return _repository.GetSearchPageList(query, pageIndex, pageSize);
        }

        /// <summary>
        /// 获得用户数据（分页）
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<UserInfo> GetPageList(int pageIndex, int pageSize)
        {
            return _repository.GetPageList(pageIndex, pageSize);
        }

        /// <summary>
        /// 获得所有用户数据
        /// </summary>
        /// <returns></returns>
        public List<UserInfo> GetList()
        {
            return _repository.GetList();
        }

        /// <summary>
        /// 通过主键获得用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserInfo Get(int id)
        {
            return _repository.Get(id);
        }

        /// <summary>
        /// 添加用户信息，并返回插入的id
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Add(UserInfo entity)
        {
            return _repository.Add(entity);
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(UserInfo entity)
        {
            return _repository.Update(entity);
        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Del(int id)
        {
            return _repository.Del(id);
        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool Dels(dynamic[] ids)
        {
            return _repository.Dels(ids);
        }
    }
}